package OfferLanQiaoBei.a_14届蓝桥真题;

import java.util.Scanner;

public class a求阶乘后若干位test {
	public char[] getNum() {
		System.out.println("请输入一个大于1的数");
		Scanner i = new Scanner(System.in);
		Long n = i.nextLong();
		if(n<=1) {
			System.out.println("您输入的数太小");
			return null;
		}
		char arr1[] = new char[n.toString().length()];
		for(int j =arr1.length-1;j>=0;j--) {
			arr1[j] = n.toString().charAt(j);
		}
		char temp[] = new char[arr1.length];;
		for(int x=0;x<arr1.length;x++){
		     temp[x] = arr1[arr1.length-x-1];
	    }

		for(Long m = n;m>0;m--) {
			int len = temp.length;
			long temp1[] = new long[len];
			for(Integer x = 0;x<len;x++) {
				if(m==1) {
					return temp;
				}
				temp1[x] = (temp[x]-'0')*(m-1);
		}
//			for (int q = 0; q< temp1.length / 2; q++) {
//	            //进行数组互换
//	            int sum = temp1[q];
//	            temp1[q] = temp1[temp1.length - 1 - q];
//	            temp1[temp1.length - 1 - q] = sum;
//	        }
//			int arr1[] = temp1;;
//			if() {
//				for(int x=0;x<temp1.length;x++){
//					arr1[x] = temp1[temp1.length-x-1];
//		     	}
//			}
			char tempx[] = this.getjin(temp1);
			temp = tempx;
		}
		return temp;
	}
	
	public char[] getjin(long temp[]) {
		int index = temp.length-1;
		for(int i=0;i<=index;i++) {
			if(temp[i]/10>=1) {
				if(i==index) {
			    	long next = temp[i]/10;
			    	long newtemp[] = new long[i+2];
			    	newtemp[i] = temp[i]%10;
			    	newtemp[i+1] = temp[i]/10;
			    	System.arraycopy(temp, 0, newtemp, 0, i);
			    	temp = new long[newtemp.length];
			    	temp = newtemp;
			    	index++;
				}else{
					temp[i+1] = temp[i]/10+temp[i+1];
					temp[i] = temp[i]%10;
			   }
			}else {
				temp[i]=temp[i];
				
			}
//			if(temp[temp.length]>=10) {
//				getjin(temp);		
//			}
		}
		char arr1[] = new char[temp.length];
		for(int z = temp.length-1; z>=0;z--) {
			arr1[z] = Character.forDigit((int) temp[z],10);
		}
		
		return arr1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("-----开始计算-----");
	    long startTime = System.currentTimeMillis();
	    a求阶乘后若干位test test = new a求阶乘后若干位test();
		char arr[] = test.getNum();
		System.out.println("阶乘的结果为:");
//		for(int i = arr.length-1;i>=0;i--) {
//			System.out.print(arr[i]);
//		}
		System.out.println("\n");
		System.out.println("结果的总长度为:"+arr.length);
		long endTime = System.currentTimeMillis();
		System.out.println("所用时间:"+(endTime-startTime)/1000+"s");
		System.out.println("-----结束-----");
	}
	
}

